<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/10/30
 * Time: 17:11
 */

namespace app\api\controller;


use think\Controller;
use think\exception\HttpException;

class Api extends Controller
{
    /**
     * @param $msg string 返回的消息
     * @param $code bool true|false 状态码
     * @param array $data 需要返回的json数据
     */
    public function json_return($msg,$code=true,$data=[]){
        $datas=[
            'message'=>$msg,
            'code'=>$code,
            'data'=>$data
        ];
        return exit(json_encode($datas,JSON_UNESCAPED_UNICODE));
    }

    /**
     * @param array $param
     * @return mixed
     * 检查请求参数
     */
    public function checkParam($param = '')
    {
        $param   = explode(',',$param);
        if(count($param) == 0){
            throw new HttpException(200,'CheckParam调用错误');
        }

        foreach ($param as $item) {
            if($this->request->has($item,'param',true) === false){
                if(!config('app_debug')){
                     $this->json_return($item.'不能为空');
                }
                throw new HttpException(400, $item.'不能为空');
            }
        }

        return $this->request->param();
    }

}